package youleche.com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.LinkedList;
import java.util.List;

import youleche.com.idao.IPagelimit;
import youleche.com.model.MPagelimit;

/**
 * 页面权限数据访问层
 * @author Administrator
 *
 */
public class DPagelimit implements IPagelimit{

	/**
	 * 添加权限页面
	 * @param con 数据库连接
	 * @param pagelimit 页面权限信息
	 * @return 返回影响的行数
	 * @throws Exception 异常
	 */
	@Override
	public int addPageLimit(Connection con, MPagelimit pagelimit)
			throws Exception {
		String sql = "INSERT pagelimit(id,pageName,pageUrl,parentId,remark,addMan,limitLevel,nodeType,A1,A2,A3,A4,A5,A6,A7,A8,addTime) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
		PreparedStatement pre = con.prepareStatement(sql);
		pre.setObject(1, pagelimit.getId());
		pre.setObject(2, pagelimit.getPageName());
		pre.setObject(3, pagelimit.getPageUrl());
		pre.setObject(4, pagelimit.getParentId());
		pre.setObject(5, pagelimit.getRemark());
		pre.setObject(6, pagelimit.getAddMan());
		pre.setObject(7, pagelimit.getLimitLevel());
		pre.setObject(8, pagelimit.getNodeType());
		pre.setObject(9, pagelimit.getA1());
		pre.setObject(10, pagelimit.getA2());
		pre.setObject(11, pagelimit.getA3());
		pre.setObject(12, pagelimit.getA4());
		pre.setObject(13, pagelimit.getA5());
		pre.setObject(14, pagelimit.getA6());
		pre.setObject(15, pagelimit.getA7());
		pre.setObject(16, pagelimit.getA8());
		pre.setObject(17, pagelimit.getAddTime());

		return pre.executeUpdate();
	}

	/**
	 * 判断是否存在该页面权限节点
	 * @param con 数据库名称
	 * @param pageName 节点名称
	 * @return 是否存在  true-存在
	 * @throws Exception 异常
	 */
	@Override
	public boolean isExistPageByPageName(Connection con, String pageName)
			throws Exception {
		String sql = "select id from pagelimit where a1='0' and pageName='"+pageName+"' limit 1;";
		PreparedStatement ps = con.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		boolean result = false;
		if(rs != null){
			while(rs.next()){
				if(!"".equals(rs.getString(1))){
					result = true;
				}
				break;
			}
		}
		
		return result;
	}

	/**
	 * 得到所有权限节点
	 * @param con 数据库连接
	 * @return 返回所有节点
	 * @throws Exception 异常
	 */
	@Override
	public List<MPagelimit> getAllPageLimitNodes(Connection con)
			throws Exception {
		String sql = "select " +
				"id,pageName,pageUrl,parentId,remark" +
				",addMan,limitLevel,nodeType,addTime,A2" +
				",A3,A4,A5,A6,A7," +
				"A8 " +
				"from pagelimit " +
				"where A1='0'";
		PreparedStatement ps = con.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		List<MPagelimit> list = null;
		MPagelimit page = null;
		if(rs != null){
			list = new LinkedList<MPagelimit>();
			while(rs.next()){
				page = new MPagelimit();
				page.setId(rs.getString(1));
				page.setPageName(rs.getString(2));
				page.setPageUrl(rs.getString(3));
				page.setParentId(rs.getString(4));
				page.setRemark(rs.getString(5));
				page.setAddMan(rs.getString(6));
				page.setLimitLevel(rs.getString(7));
				page.setNodeType(rs.getString(8));
				page.setAddTime(rs.getDate(9));
				page.setA2(rs.getString(10));
				page.setA3(rs.getString(11));
				page.setA4(rs.getString(12));
				page.setA5(rs.getString(13));
				page.setA6(rs.getString(14));
				page.setA7(rs.getString(15));
				page.setA8(rs.getString(16));
				page.setA1("0");
				list.add(page);
			}
		}
		
		return list;
	}

	/**
	 * 通过id更新节点
	 * @param con 数据库连接
	 * @param id id
	 * @return 返回影响的行数
	 * @throws Exception 异常
	 */
	@Override
	public int updateNodeById(Connection con, String id) throws Exception {
		String sql = "";
		return 0;
	}

}
